package org.rcy.framework.thrift.struct;

import org.apache.thrift.TException;

import java.lang.reflect.Type;

/**
 * TBase的返回结果实现
 * 
 * @author zhouree
 */
public class ResultStruct extends AbstractStruct {

	private static final long serialVersionUID = 1L;

	private static final String SUCCESS = "success";
	private static final String EXCEPTION = "exception";
	private static final short SUCCESS_ID = 0;
	private static final short EXCEPTION_ID = 1;

	//返回值类型
	private final FieldIdEnum success = new FieldIdEnum(SUCCESS_ID, SUCCESS);
	private final FieldIdEnum exception = new FieldIdEnum(EXCEPTION_ID, EXCEPTION);

	public ResultStruct(Type returnType) throws TException {
		FieldMetaData successMetaData = MetaDataBuilder.fieldMetaData(success, returnType,null);
		addMetaData(success, successMetaData);
		
		FieldMetaData exceptionMetaData = MetaDataBuilder.fieldMetaData(exception, ExceptionData.class,null);
		addMetaData(exception, exceptionMetaData);
	}

	public void setSuccess(Object value) {
		this.setFieldValue(success, value);
	}

	public Object getSuccess(){
		Object result = this.getFieldValue(success);
		if (result == null) {
			logger.debug("MISSING_RESULT: null result");
		}
		return result;
	}
	
	public void setException(Exception e) {
		ExceptionData ed = new ExceptionData(e);
		this.setFieldValue(exception, ed);
	}
	
	public Exception getException() {
		ExceptionData ed = (ExceptionData) this.getFieldValue(exception);
		return ed != null ? ed.getException() : null;
	}
}
